package service4admin

import (
	"Gin-Gorm-boke/boke_admin/models4admin"
	"Gin-Gorm-boke/boke_common/models"
	"Gin-Gorm-boke/boke_common/response"
	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/jinzhu/copier"
	"strconv"
)

func GetRoleList(c *gin.Context) {
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	roleName := c.Query("roleName")
	status := c.Query("status")
	var total int64
	var roleList []models4admin.Role
	db := models.Db.Model(new(models4admin.Role))
	if roleName != "" {
		db.Where("role_name like ?", "%"+roleName+"%")
	}
	if status != "" {
		db.Where("status = ?", status)
	}
	db.Count(&total)
	db.Order("role_sort asc").Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&roleList)
	response.ResponseOK(c, gin.H{
		"total": total,
		"rows":  roleList,
	})
	return

}

func UpdateStatus(c *gin.Context) {
	var role models4admin.Role
	_ = c.ShouldBindJSON(&role)
	models.Db.Model(new(models4admin.Role)).Where("id = ?", role.Id).Update("status", role.Status)
	response.ResponseOK(c, nil)
	return
}

func SaveRole(c *gin.Context) {
	var roleDto models4admin.RoleDto
	var role models4admin.Role
	if err := c.ShouldBindJSON(&roleDto); err != nil {
		response.ResponseBadRequest(c)
		return
	}
	copier.Copy(&role, &roleDto)
	var menuIds []null.Int
	menuIds = roleDto.MenuIds
	models.Db.Model(new(models4admin.Role)).Create(&role)
	for _, v := range menuIds {
		var roleMenu models4admin.RoleMenu
		roleMenu.RoleId = role.Id
		roleMenu.MenuId = int(v.Int64)
		models.Db.Model(new(models4admin.RoleMenu)).Create(&roleMenu)
	}
	response.ResponseOK(c, nil)
	return
}

func GetRoleById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var role models4admin.Role
	models.Db.Model(new(models4admin.Role)).Where("id=?", id).First(&role)
	response.ResponseOK(c, role)
	return
}

func UpdateRole(c *gin.Context) {
	var roleDto models4admin.RoleDto
	var role models4admin.Role
	if err := c.ShouldBindJSON(&roleDto); err != nil {
		response.ResponseBadRequest(c)
		return
	}
	copier.Copy(&role, &roleDto)
	var menuIds []null.Int
	menuIds = roleDto.MenuIds
	models.Db.Model(new(models4admin.Role)).Where("id=?", role.Id).Updates(&role)
	if len(menuIds) > 0 {
		sql := "delete from sys_role_menu where role_id = ?"
		models.Db.Exec(sql, role.Id)
	}
	for _, v := range menuIds {
		var roleMenu models4admin.RoleMenu
		roleMenu.RoleId = role.Id
		roleMenu.MenuId = int(v.Int64)
		models.Db.Model(new(models4admin.RoleMenu)).Create(&roleMenu)
	}
	response.ResponseOK(c, nil)
	return
}

func DelRole(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	models.Db.Model(new(models4admin.Role)).Where("id=?", id).Delete(models4admin.Role{})
	models.Db.Model(new(models4admin.RoleMenu)).Where("role_id=?", id).Delete(models4admin.RoleMenu{})
	response.ResponseOK(c, nil)
	return
}

func ListAllRole(c *gin.Context) {
	var roleList []models4admin.Role
	models.Db.Model(new(models4admin.Role)).Find(&roleList)
	response.ResponseOK(c, roleList)
	return
}
